package com.allenyuan.carvingsocketclient;

import java.util.List;

import com.allenyuan.proto.GCodeOperation;
import com.allenyuan.proto.PrintTask;
import com.allenyuan.proto.TestPbProto;

public class RemotingMethod {
	
	private static AsyncSocketClient client = null;
	private static final String SERVER_IP = "192.168.1.20";
	private static final int SERVER_PORT = 5000;

	private static void InvokeRemotingMethod(com.google.protobuf.Message message, AsyncSocketClient.ProgressListener listener) {
		client = new AsyncSocketClient();
		client.setProgressListener(listener);
		client.setMessage(message);
		client.connect(SERVER_IP, SERVER_PORT);
	}
	
	public static void TestPb(int i, AsyncSocketClient.ProgressListener listener) {
		TestPbProto.TestPb.Builder builder = TestPbProto.TestPb.newBuilder();
		builder.setTest(i);
		InvokeRemotingMethod(builder.build(), listener);
	}
	
	public static void GCodePackage(List<String> lines, int packageNo, int totalPackages, int taskId, AsyncSocketClient.ProgressListener listener) {
		GCodeOperation.GCodePackage.Builder builder = GCodeOperation.GCodePackage.newBuilder();
		builder.addAllGcodeLine(lines);
		builder.setPackageNo(packageNo);
		builder.setTotalPackages(totalPackages);
		builder.setTaskId(taskId);
		InvokeRemotingMethod(builder.build(), listener);
	}
	
	public static void AppendPrintTask(PrintTask.PrintTaskType printTaskType, int height, int width, boolean isUrgent, AsyncSocketClient.ProgressListener listener) {
		PrintTask.AppendPrintTask.Builder builder = PrintTask.AppendPrintTask.newBuilder();
		builder.setPrintTaskType(printTaskType);
		builder.setHeight(height);
		builder.setWidth(width);
		builder.setIsUrgent(isUrgent);
		InvokeRemotingMethod(builder.build(), listener);
	}
	
	public static void SetPrintTask(int id, boolean isUrgent, AsyncSocketClient.ProgressListener listener) {
		PrintTask.SetPrintTask.Builder builder = PrintTask.SetPrintTask.newBuilder();
		builder.setTaskId(id);
		builder.setIsUrgent(isUrgent);
		InvokeRemotingMethod(builder.build(), listener);
	}
	
	public static void RemovePrintTask(int id, AsyncSocketClient.ProgressListener listener) {
		PrintTask.RemovePrintTask.Builder builder = PrintTask.RemovePrintTask.newBuilder();
		builder.setTaskId(id);
		InvokeRemotingMethod(builder.build(), listener);
	}
	
	public static void GetPrintTask(int id, AsyncSocketClient.ProgressListener listener) {
		PrintTask.GetPrintTask.Builder builder = PrintTask.GetPrintTask.newBuilder();
		builder.setTaskId(id);
		InvokeRemotingMethod(builder.build(), listener);
	}
	
	public static void GetPrintTaskPosition(int id, AsyncSocketClient.ProgressListener listener) {
		PrintTask.GetPrintTaskPosition.Builder builder = PrintTask.GetPrintTaskPosition.newBuilder();
		builder.setTaskId(id);
		InvokeRemotingMethod(builder.build(), listener);
	}

}
